import api from "@/api";

export default {
  namespace: "role",
  state: {
    sysRoots: [],
    roleList: [],
    roleUserList: {
      list: [],
      pagination: {},
    },
    roleTreeList: [],
    userList: {
      list: [],
      pagination: {},
    },
    treeData: [],
    checkedTreeNode: [],
    orgTree: [],
    groupList: [],

    roleIdss: "",
    newroleIdss: "",
    userIdss: "",
    keyIdss: "",
    direction: "",
    checkedList: "",
    newCheck: "",
    editOrgTreeList: [],
    countLength: "",
  },

  effects: {
    // *fetchSysRoots(_, { call, put }) {
    //   const response = yield call(api.role.querySysRoots);
    //   if (response.errorCode === 0) {
    //     yield put({
    //       type: 'saveSysRoots',
    //       payload: response.data,
    //     });
    //   }
    //   return response;
    // },
    *fetchRoleList({ payload }, { call, put }) {
      const response = yield call(api.role.queryRoleList, payload);
      if (response.code === 200) {
        yield put({
          type: "saveRoleList",
          payload: response.data,
        });
      }
      return response;
    },
    // 添加角色分组的角色权限
    *createRole({ payload }, { call, put }) {
      const response = yield call(api.role.create, payload);
      if (response.code === 200) {
        yield put({
          type: "saveTreeData",
          payload: response.data,
        });
      }
      console.log(response);
      return response;
    },
    // 添加角色分组的确定按钮
    *addSureRole({ payload }, { call, put }) {
      return yield call(api.role.addSure, payload);
    },
    *deleteRole({ payload }, { call, put }) {
      return yield call(api.role.delete, payload);
    },
    *updateRole({ payload }, { call, put }) {
      return yield call(api.role.update, payload);
    },
    *queryMembersByRole({ payload }, { call, put }) {
      const response = yield call(api.role.queryMembersByRole, payload);
      if (response.code === 200) {
        yield put({
          type: "saveRoleUserList",
          payload: {
            list: response.data.querySysUserDto.resultList,
            // pagination: response.pagination,
            totalCount: response.data.querySysUserDto.totalCount,
          },
        });
      }
      return response;
    },
    *deleteMembers({ payload }, { call, put }) {
      return yield call(api.role.deleteMembers, payload);
    },
    *fetchUserList({ payload }, { call, put }) {
      const response = yield call(api.role.queryUsers, payload);
      if (response.errorCode === 0) {
        yield put({
          type: "saveUserList",
          payload: {
            list: response.data,
            pagination: response.pagination,
          },
        });
      }
      return response;
    },
    *addUsers({ payload }, { call, put }) {
      return yield call(api.role.addUsers, payload);
    },
    *validateRoleCode({ payload }, { call, put }) {
      return yield call(api.role.validateRoleCode, payload);
    },
    *updateRoleTree({ payload }, { call, put }) {
      return yield call(api.role.updateRoleTree, payload);
    },
    *fetchRoleTree({ payload }, { call, put }) {
      const response = yield call(api.role.queryRoleTree, payload);
      if (response.code === 200) {
        yield put({
          type: "saveTreeData",
          payload: [response.data],
        });
        const checkedTreeNode = [];
        const getCheckedTreeNode = (treeData) => {
          treeData.forEach((item) => {
            item.selected && checkedTreeNode.push(item.id);
            if (item.children && item.children.length > 0) {
              getCheckedTreeNode(item.children);
            }
          });
          return checkedTreeNode;
        };
        yield put({
          type: "saveCheckedTreeNode",
          payload: getCheckedTreeNode([response.data]),
        });
      }
    },
    *editOrgTree({ payload }, { call, put }) {
      const response = yield call(api.role.editRoleTree, payload);
      if (response.code === 200) {
        yield put({
          type: "saveeditOrgTree",
          payload: [response.data],
        });
        // const checkedTreeNode = [];
        // const getEditTreeNode = editOrgTree => {
        //   editOrgTree.forEach(item => {
        //     item.selected && checkedTreeNode.push(item.id);
        //     if (item.children && item.children.length > 0) {
        //       getEditTreeNode(item.children);
        //     }
        //   });
        //   return EditTreeNode;
        // };
        // yield put({
        //   type: 'saveEditTreeNode',
        //   payload: getEditTreeNode([response.data]),
        // });
      }
      return response;
    },
    *queryOrgTree({ payload }, { call, put }) {
      const response = yield call(api.role.queryOrgTree, payload);
      if (response.errorCode === 0) {
        yield put({
          type: "saveOrgTree",
          payload: response.data,
        });
      }
      return response;
    },
    *queryGroupList({ payload }, { call, put }) {
      const response = yield call(api.role.queryGroupList, payload);
      if (response.errorCode === 0) {
        yield put({
          type: "saveGroupList",
          payload: response.data,
        });
      }
      return response;
    },
    *updateRoleOrg({ payload }, { call, put }) {
      return yield call(api.role.updateRoleOrg, payload);
    },
    *updateRoleGroup({ payload }, { call, put }) {
      return yield call(api.role.updateRoleGroup, payload);
    },
    *updateRoleUsers({ payload }, { call, put }) {
      return yield call(api.role.updateRoleUsers, payload);
    },
  },

  reducers: {
    saveSysRoots(state, { payload }) {
      return {
        ...state,
        sysRoots: payload,
      };
    },
    saveRoleList(state, { payload }) {
      return {
        ...state,
        roleList: payload,
      };
    },
    saveTreeData(state, { payload }) {
      return {
        ...state,
        treeData: payload,
      };
    },
    saveeditOrgTree(state, { payload }) {
      return {
        ...state,
        orgList: payload,
      };
    },

    saveRoleUserList(state, { payload }) {
      return {
        ...state,
        roleUserList: payload,
      };
    },

    saveUserList(state, { payload }) {
      return {
        ...state,
        userList: payload,
      };
    },
    saveTreeData(state, { payload }) {
      return {
        ...state,
        treeData: payload,
      };
    },
    saveCheckedTreeNode(state, { payload }) {
      return {
        ...state,
        checkedTreeNode: payload,
      };
    },

    // saveEditTreeNode(state, { payload }) {
    //   return {
    //     ...state,
    //     EditTreeNode: payload,
    //   };
    // },

    saveOrgTree(state, { payload }) {
      return {
        ...state,
        orgTree: payload,
      };
    },
    saveGroupList(state, { payload }) {
      return {
        ...state,
        groupList: payload,
      };
    },
    changeRoleId(state, { payload }) {
      return {
        ...state,
        roleIdss: payload.id,
      };
    },
    changeNewRoleId(state, { payload }) {
      return {
        ...state,
        newroleIdss: payload.id,
      };
    },
    changeUserIdss(state, { payload }) {
      return {
        ...state,
        userIdss: payload.id,
      };
    },
    changeKeyIdss(state, { payload }) {
      return {
        ...state,
        keyIdss: payload.id,
      };
    },
    changedirection(state, { payload }) {
      return {
        ...state,
        direction: payload.id,
      };
    },
    newcheckedList(state, { payload }) {
      return {
        ...state,
        checkedList: payload.id,
      };
    },
    newchecked(state, { payload }) {
      return {
        ...state,
        newCheck: payload.id,
      };
    },
    editOrgTreeList(state, { payload }) {
      return {
        ...state,
        editOrgTreeList: payload,
      };
    },
    StartcountLength(state, { payload }) {
      return {
        ...state,
        countLength: payload.id,
      };
    },
  },
};
